﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
using BOHVote.Entity;
 

namespace BOH.AccreditationSystem
{
    public partial class InterviewQuestions : System.Web.UI.Page
    {
        BHUser user = null;
        private string UserID = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string SubjectCode = string.Empty;
                if (Session["User"] != null)
                {
                    user = (BHUser)Session["User"];
                    if (user.RightType == "S")
                    {
                        SubjectCode = user.RightCode;
                        //QuestionDataBind();
                        LoadDrop("",SubjectCode);
                        
                    }
                    else if (user.RightType == "C" || user.RightType == "A" || user.RightType == "Q")
                    {
                        //QuestionDataBind();
                        LoadDrop("","");
                        
                    }
                    UserID = user.UserID;

                }
                else
                {

                }
                
            }
        }

        #region <<  绑定DropdownList  >>

        
        public void LoadDrop(string level,string subjectCode)
        {
            SubjectBind(level,subjectCode);
            
            //BindApplyStyle();
            //getJobRank();
            //BindPeriod();
        }

        //public void getJobRank()
        //{
        //    drpQuestionLevel.Items.Clear();
        //    ListItem li1 = new ListItem("请选择级别", "-1");
        //    this.drpQuestionLevel.Items.Add(li1);
        //    DataSet dst = OtherClass.getJobRank();
        //    foreach (DataRow dr in dst.Tables[0].Rows)
        //    {
        //        ListItem li = new ListItem(dr["JobRankName"].ToString(),  dr["JobRankLevel"].ToString());
        //        drpQuestionLevel.Items.Add(li);
        //    }
        //}

        public void SubjectBind(string level,string subjectID)
        {
            this.drpSubject.Items.Clear();
            DataSet dst = null;
            string sbujectName = string.Empty;
            if (String.IsNullOrEmpty(level) && String.IsNullOrEmpty(subjectID))
                dst = Subject.getAllASubject();
            else if(String.IsNullOrEmpty(subjectID))
                dst =Subject.getSubjectByLevel(level);
            if(!String.IsNullOrEmpty(subjectID))
            {
                sbujectName = Subject.getSubjectNameBySubjectID(subjectID);
            }
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            if (dst != null)
            {
                foreach (DataRow dr in dst.Tables[0].Rows)
                {
                    if (dr["SubjectID"].ToString().Length > 0)
                    {
                        ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                        this.drpSubject.Items.Add(li);
                    }
                }
            }
            if (!String.IsNullOrEmpty(sbujectName))
            {
                ListItem lisubject = new ListItem(sbujectName, subjectID);
                this.drpSubject.Items.Add(lisubject);
            }
        }

        protected void drpSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSubjectID = ((DropDownList)sender).SelectedValue;
            if (strSubjectID != "-1")
            {
                SpecialityBind(strSubjectID);
            }
            else
            {
                drpSpeciality.Items.Clear();
                ListItem li1 = new ListItem("请选择专业", "-1");
                this.drpSpeciality.Items.Add(li1);
            }
        }

        public void SpecialityBind(string SubjectID)
        {
            drpSpeciality.Items.Clear();
            ListItem li1 = new ListItem("请选择专业", "-1");
            this.drpSpeciality.Items.Add(li1);
            ListItem li2 = new ListItem("全部专业[共性题]", "0");
            this.drpSpeciality.Items.Add(li2);

            DataSet dst = Subject.getAllTestSpecialityBySubjectID(SubjectID);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["TestSpecialityName"].ToString(), dr["TestSpecialityID"].ToString());
                this.drpSpeciality.Items.Add(li);
            }
            //DataSet dst = Subject.getAllSpecialityBySubjectID(SubjectID);
            //foreach (DataRow dr in dst.Tables[0].Rows)
            //{
            //    ListItem li = new ListItem(dr["SpecialityName"].ToString(), dr["SpecialityID"].ToString());
            //    this.drpSpeciality.Items.Add(li);
            //}
        }

        //public void BindApplyStyle()
        //{
        //    DataSet dst = OtherClass.dstGetApplyStyle();
        //    BindDropDownList(drpApplyStyle, dst);
        //}

        public void BindDropDownList(ListControl Controls, DataSet dst)
        {
            Controls.Items.Clear();
            ListItem list = new ListItem("请选择", "-1");
            Controls.Items.Add(list);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem itemlist = new ListItem(dr["dict_Display"].ToString(), dr["dict_Value"].ToString());
                Controls.Items.Add(itemlist);
            }
        }
        
        #endregion

        #region <<  binding GridView  >>

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            QuestionDataBind();
        }

        public void QuestionDataBind()
        {
            int strQuestionLevel = -1;
            string strSubject = string.Empty;
            string strSpeciality = string.Empty;

            if (drpSubject.SelectedValue.Trim() != "-1")
                strSubject = drpSubject.SelectedValue.Trim();
            //if (drpSpeciality.SelectedValue.Trim() != "-1")
            //    strSpeciality = drpSpeciality.SelectedValue.Trim();
            DataSet dst = InterviewClass.getQuestions(txtQuestionContent.Text.Trim(), strSubject, drpSpeciality.SelectedValue.Trim(), "", (string)Session["ApplyPeriod"]);
            QuestionBind(dst);
        }

        public void QuestionBind(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    spanTotal.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = dst.Tables[0].DefaultView;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    labTotalNo.Text = AspNetPager1.RecordCount.ToString();
                    InterviewQuestionsList.DataSource = pds;
                    InterviewQuestionsList.DataBind();
                }
                else
                {
                    spanTotal.Visible = false;
                    InterviewQuestionsList.DataSource = null;
                    InterviewQuestionsList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                }
            }
            else
            {
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                InterviewQuestionsList.DataSource = null;
                InterviewQuestionsList.DataBind();
            }
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
             QuestionDataBind();
        }

        #endregion

        protected void InterviewQuestionsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow dr = this.InterviewQuestionsList.Rows[e.RowIndex];
            string QID = ((HiddenField)dr.FindControl("hidQID")).Value;
            InterviewClass.DelQuestionByID(QID);

            QuestionDataBind();
        }


    }
}
